package demo11;

public class Day1 {
    //双指针类型题：
    //题一：
    public void func(int[] array) {
        int cur = 0;
        int dest = 0;
        for(int i = 0; i < array.length; i++) {
            if(array[i] != 0) {
                swap(array, cur, dest);
                dest++;
            }
            cur++;
        }
    }
    private void swap(int[] array, int cur, int dest) {
        int tmp = array[cur];
        array[cur] = array[dest];
        array[dest] = tmp;
    }

    //题二：
    public void func2(int[] array) {
        int count = 0;
        int cur = 0;
        for(cur = 0; cur < array.length; cur++) {
            if(array[cur] == 0) {
                count++;
            }
            count++;
            if(count >= array.length) {
                break;
            }
        }
        //已经找到了覆写的最后一个数：cur
        int dest = array.length - 1;
        while(cur != 0) {
            if(array[cur] != 0) {
                array[dest--] = array[cur];
            }else {
                for(int i = 0; i < 2; i++) {
                    array[dest--] = 0;
                }
            }
            cur--;
        }
    }
}
